package com.itboye.sunsun.volley;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.HttpHeaderParser;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.itboye.sunsun.utils.DataEncryptionUtil;

/**
 * 建造者模式创建Request  
 * @author Young
 *
 * @param <T> 请求结束后返回的对象，可以是 List<自定义类>  或  自定义类  
 */
public class XJsonRequest<T> extends Request<T> {

	private Listener<T> listener;

	private Map<String,String>headers=new HashMap<String, String>();
	private int method=Method.POST;
	private Map<String,String>params=new HashMap<String, String>();
	/**
	 * 
	 * @param url
	 * @param listener
	 * @param errlistener
	 */
	public XJsonRequest(String url, XRequestListener<T> listener, ErrorListener errlistener) {
		super(Method.GET, url, errlistener);
		this.listener = listener;
	}

	@Override
	protected void deliverResponse(T response) {
		if (null != listener) {
			listener.onResponse(response);
		}
	}

	public XJsonRequest<T>addHeader(String k,String value){
		headers.put(k, value);
		return this;
	}
	@Override
	public Map<String, String> getHeaders() throws AuthFailureError {
		return  headers;
	}
	
	public XJsonRequest<T>setMethod(int method){
		this.method=method;
		return this;
	}
	@Override
	public int getMethod() {
		return  method;
	}
	
	public XJsonRequest<T>addParam(String k,String value){
		params.put(k, value);
		return this;
	}
	@Override
	protected Map<String, String> getParams() throws AuthFailureError {
		return  params;
	}
	
	
	//该方法中gson解析可能需要调整
	@Override
	protected Response<T> parseNetworkResponse(NetworkResponse response) {
		String parsed;
		try {
			parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
		} catch (UnsupportedEncodingException e) {
			parsed = new String(response.data);
		}

		
		if (listener != null) {
			Type typeClass1 = listener.getClass().getGenericSuperclass();
			if (typeClass1 instanceof ParameterizedType) {
				Type actualType1 = ((ParameterizedType) typeClass1).getActualTypeArguments()[0];

				System.out.println("type==="+actualType1);
//				Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
//				T res = gson.fromJson(parsed, actualType1);
				
				JSONObject jsonObject;
				try {
					jsonObject = new JSONObject(parsed);
					String resultData = jsonObject.getString("data");
					
					 parsed = DataEncryptionUtil.decodeData(resultData);
					 
					jsonObject = new JSONObject(parsed);
					int code = jsonObject.getInt("code");
					if (code!=0) {
						return null;
					}
					 resultData = jsonObject.getString("data");
					
					T res = new Gson().fromJson(resultData, actualType1);

					if (null!=res) {
						return Response.success(res, HttpHeaderParser.parseCacheHeaders(response));
					}
					
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				

			}
		}
		return null;

	}

}
